<?php

namespace app\index\controller\client;

use ShidianUtil\Thinkphp\Response\ApiResponse;
use ShidianUtil\Utils\CheckUtil;
use app\index\model\UM as UM;
use app\index\service\CacheService;
use think\APP;
use think\Request as Request;

/**
 * Cache 控制器
 * Class Cache
 * @package app\index\controller\client;
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 * @see \app\index\validate\client\Cache
 */
class Cache
{
    // 请求参数
    private Request $request;

    // service
    private CacheService $service;

    public function __construct(Request $request, CacheService $cacheService)
    {
        $this->request = $request;
        $this->service = $cacheService;
    }

    /**
     * 详情
     * @param UM|null $user
     * @return ApiResponse
     * @url /client/cache/read
     */
    public function read(?UM $user): ApiResponse
    {
        $request     = $this->request;

        $id = $request->param('id');

        if(!CheckUtil::isId($id)){
            return ApiResponse::fail('缺少id参数');
        }

        return ApiResponse::success($this->service->read($user, $id));
    }

    /**
     * 新增|修改
     * @param UM $user
     * @return ApiResponse
     * @url /client/cache/save
     */
    public function save(?UM $user): ApiResponse
    {
        $request     = $this->request;

        $data = $this->service->add($user, $request->param());

        return ApiResponse::success($data);
    }

}
